package psn.kiko.controller.operation;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import psn.kiko.annotation.api.StableID;
import psn.kiko.pojo.Result;
import psn.kiko.pojo.dto.OperationDTO;
import psn.kiko.pojo.vo.LabelValue;
import psn.kiko.pojo.vo.OperationVO;
import psn.kiko.pojo.vo.PureInterfaceVO;
import psn.kiko.service.business.system.OperationService;

import java.util.List;

/**
 * 导航项操作
 */
@RestController
@RequestMapping("/admin/operation")
@Api(tags = {"导航项操作管理"})
public class OperationController{
	@Autowired
	OperationService operationService;
	
	@GetMapping("/operations/{navItemId}")
	@ApiOperation("获取导航项的操作列表")
	@StableID(201)
	public Result operationsOfNavItem(@PathVariable("navItemId") @ApiParam("导航项id") Integer navItemId){
		List<OperationVO> operations=operationService.getOperationsOfNavItem(navItemId);
		return Result.success(operations);
	}
	
	@GetMapping("/getInterfaceSelection")
	@ApiOperation("获取接口下拉选项")
	@StableID(202)
	public Result getInterfaceSelection(){
		List<LabelValue> interfaceSelection=operationService.getAllInterfaceSelection();
		return Result.success(interfaceSelection);
	}
	
	@GetMapping("/operation/{operationId}")
	@ApiOperation("根据主键获取导航项操作")
	@StableID(203)
	public Result operation(@PathVariable("operationId") @ApiParam("操作id") Integer operationId){
		OperationVO operationVO=operationService.getOperation(operationId);
		return Result.success(operationVO);
	}
	
	@PostMapping("/addOprationOfNavItem/{navItemId}")
	@ApiOperation("新增导航项的一个操作")
	@StableID(204)
	public Result addOperation(
			@PathVariable("navItemId") @ApiParam("导航项id") Integer navItemId,
			@RequestBody @ApiParam("操作DTO") OperationDTO operationDTO){
		Integer r=operationService.addOperationOfNavItem(operationDTO,navItemId);
		return Result.success(r);
	}
	
	@PutMapping("/editById")
	@ApiOperation("编辑操作")
	@StableID(205)
	public Result editOperationById(
			@RequestBody @ApiParam("操作DTO") OperationDTO operationDTO){
		Integer r=operationService.updateOperation(operationDTO);
		return Result.success(r);
	}
	
	@DeleteMapping("/batchDeleteById")
	@ApiOperation("根据主键批量删除操作")
	@StableID(206)
	public Result batchDeleteById(@RequestBody @ApiParam("操作id集合") List<Integer> opIds){
		Integer r=operationService.deleteOperations(opIds);
		return Result.success(r);
	}
	
	@PutMapping("/batchEnableById")
	@ApiOperation("根据主键批量启用操作")
	@StableID(207)
	public Result batchEnableById(@RequestBody @ApiParam("操作id集合") List<Integer> opIds){
		Integer r=operationService.batchEnableOperations(opIds);
		return Result.success(r);
	}
	
	@PutMapping("/batchDisableById")
	@ApiOperation("根据主键批量禁用操作")
	@StableID(208)
	public Result batchDisableById(@RequestBody @ApiParam("操作id集合") List<Integer> opIds){
		Integer r=operationService.batchDisableOperations(opIds);
		return Result.success(r);
	}
	
	@PostMapping("/allocateApisToOperation/{operationId}")
	@ApiOperation("使一批接口与操作关联")
	@StableID(209)
	public Result allocateApisToOperation(
			@PathVariable("operationId") @ApiParam("操作id") Integer operationId,
			@RequestBody @ApiParam("接口stableId集合") List<Integer> interfaceStableIds
	){
		Integer r = operationService.allocateApisToOperation(interfaceStableIds,operationId);
		return Result.success(r);
	}
	
	@GetMapping("/associatedApisOfOperation/{operationId}")
	@ApiOperation("获取操作关联的接口")
	@StableID(210)
	public Result getAssociatedApisOfOperation(
			@PathVariable("operationId") @ApiParam("操作id") Integer operationId
	){
		List<PureInterfaceVO> associatedApisOfOperation = operationService.getAssociatedApisOfOperation(operationId);
		return Result.success(associatedApisOfOperation);
	}
}
